﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace InstaMe
{
    public enum MessageElementType
    {
        PLAIN_TEXT,
        HYPERLINK,
        EMOTICON
    }
    public class MessageLineElement
    {

        private MessageElementType type;
        private String data;
        private String link;
        private FontFamily fontFamily; //to do: hardcode font now
        private double fontSize;
        private SolidColorBrush color;

        public MessageLineElement(MessageElementType type, String data, double fontSize, FontFamily font = null, SolidColorBrush fontColor = null, string link = null)
        {
            this.type = type;
            this.data = data;
            this.link = link;
            this.fontSize = fontSize;
            if (font != null)
            {
                this.fontFamily = font;
            }
            else
            {
                this.fontFamily = new FontFamily("Seogoe UI");
            }

            if (fontColor != null)
            {
                this.color = fontColor;
            }
            else
            {
                //todo: check if type is hyperlink then use hyperlink color
                this.color = (SolidColorBrush)Application.Current.Resources["PhoneForegroundBrush"];
            }
        }

        public double getWidth()
        {
            return StringWidth(data, fontSize, fontFamily);
        }

        public double StringWidth(string str, double fontSize, FontFamily fontFamily = null)
        {
            TextBlock wrapTextBlock = null;
            FontFamily defaultFontFamily = null;
            if (wrapTextBlock == null)
            {
                wrapTextBlock = new TextBlock();
            }
            if (defaultFontFamily == null)
            {
                defaultFontFamily = new FontFamily("Seogoe UI");
            }
            wrapTextBlock.FontSize = fontSize;
            wrapTextBlock.FontFamily = fontFamily ?? defaultFontFamily;
            wrapTextBlock.Text = str;
            return Math.Ceiling(wrapTextBlock.ActualWidth);
        }

        public String getData()
        {
            return data;
        }

        public String getLink()
        {
            return link;
        }

        public MessageElementType getType()
        {
            return type;
        }

        public FontFamily getFont()
        {
            return fontFamily;
        }

        public double getFontSize()
        {
            return fontSize;
        }

        public SolidColorBrush getColor()
        {
            return color;
        }

        //to do: all of below function must recalculate line width    
        public void setData(String data)
        {
            this.data = data;
        }
    }
}
